//
//  HNStudentSignDetailVC.m
//  优学荟管理系统
//
//  Created by edz on 2017/12/9.
//  Copyright © 2017年 星道三好. All rights reserved.
//

#import "HNStudentSignDetailVC.h"
#import "HNStudentSignDetailModel.h" // Model对象
#import <UIButton+WebCache.h>
#import "ImgZoom.h"

#define KIsiPhoneX ([UIScreen instancesRespondToSelector:@selector(currentMode)] ? CGSizeEqualToSize(CGSizeMake(1125, 2436), [[UIScreen mainScreen] currentMode].size) : NO)
@interface HNStudentSignDetailVC ()
{
    __weak IBOutlet UIActivityIndicatorView *_Activity; // 加载框
    __weak IBOutlet UIView *_HideView; // 遮罩View
    __weak IBOutlet UILabel *_ChildName; // 孩子名称
    __weak IBOutlet UILabel *_RecordCount; // 操作次数
    __weak IBOutlet UILabel *_State; // 操作状态
    __weak IBOutlet UILabel *_LessionName; // 课程名称
    __weak IBOutlet UILabel *_TeacherName; // 课程老师
    __weak IBOutlet UILabel *_SignTime;  // 签到时间
    __weak IBOutlet UIView *_SignView; // 签到图片的View
    IBOutletCollection(UIButton) NSArray *SignImgs; // 图片的容器Arr
    __weak IBOutlet NSLayoutConstraint *_TopViewConstraint; // iponeX适配
    HNStudentSignDetailModel *_Model; // 该界面的所有数据放在这个对象
}
@end

@implementation HNStudentSignDetailVC

- (void)viewWillAppear:(BOOL)animated {
    
    [super viewWillAppear:animated];
    
    [self.navigationController setNavigationBarHidden:YES animated:YES];
}

- (void)viewWillDisappear:(BOOL)animated {
    
    [super viewWillDisappear:animated];
    
    [self.navigationController setNavigationBarHidden:NO animated:YES];
}

- (void)viewDidLoad {
    
    [super viewDidLoad];
    
    _TopViewConstraint.constant = KIsiPhoneX ? 30:0;
    
    // 获取数据，成功之CallBack界面赋值
    [self RefreshDataWithCallBack:^{
        // 请求成功
        // 移除遮罩
        [_HideView removeFromSuperview];
        // 赋值
        _LessionName.text = _Model.lessonName;
        _TeacherName.text = _Model.teacherName;
        _ChildName.text   = _Model.childName;
        _RecordCount.text = [_Model.recordCount stringValue];
        _State.text       = [self GetStateStrOfIndex:[_Model.status integerValue]];
        _SignTime.text    = _Model.recordTime;
        _SignView.hidden  = _Model.pictures.count == 0 ? YES:NO;
        // 图片
        [_Model.pictures enumerateObjectsUsingBlock:^(NSString * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
            
            UIButton *Button = (UIButton *)SignImgs[idx];
            [Button sd_setBackgroundImageWithURL:[NSURL URLWithString:obj] forState:(UIControlStateNormal)];
        }];
        // 隐藏没有图片的按钮
        for (NSInteger i = 0; i < SignImgs.count; i ++) {
            
            UIButton *Button = (UIButton *)SignImgs[i];
            Button.hidden = i < _Model.pictures.count ? NO:YES;
        }
    }];
    
    
    
}



#pragma mark - 网络
/** 获取数据，成功之CallBack界面赋值 */
- (void)RefreshDataWithCallBack:(void (^)())CallBack {
    
    NSDictionary *Params = @{
                             @"action"  :@"getPushInfo",
                             @"id"      :self.ID,
                             @"status"  :self.Status,
                             @"time"    :self.Time
                             };
    [NetManager Get:LLBaseURLTeacher(@"trainLesson.do") Auth:YES Params:Params Success:^(id Obj) {
        
        if ([Obj[@"code"] isEqualToNumber:@1000]) {
            
            _Model = [[HNStudentSignDetailModel alloc] init_ValuForKeyWithDict:Obj[@"t"]];
    
            dispatch_async(dispatch_get_main_queue(), ^{
            
                CallBack ? CallBack() : nil;
            });
            
        } else {
            
            [_Activity stopAnimating];
            [HUD showError:Obj[@"msg"]];
        }
    } Fail:^(NSError *Error) {
        [_Activity stopAnimating];
        [HUD showError:NetErr];
    }];
}


#pragma mark - Func
/** 根据数值返回字符串 */
- (NSString *)GetStateStrOfIndex:(NSInteger)Index {
    
    switch (Index) {
            case 0:
            return @"签到";
            case 1:
            return @"缺勤";
            case 2:
            return @"请假";
            default:
            return @"未知";
    }
}


#pragma mark - 事件
/** 退出 */
- (IBAction)Pop:(id)sender {
    
    [self.navigationController popViewControllerAnimated:YES];
}

- (IBAction)ClickImage:(UIButton *)sender {
    
    [ImgZoom MgrButtonImgView:sender];
}
@end

